/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchBar.java
 *
 * Created on 24.12.2008, 12:35:22
 */
package de.blitzcoder.collide.gui;

import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import java.awt.Color;
import org.gjt.sp.jedit.textarea.AntiAlias;
import org.gjt.sp.jedit.textarea.Selection;

/**
 *
 * @author blitzcoder
 */
public class SearchBar extends javax.swing.JPanel {

    private CollIDETextArea textArea;
    private int currentPosition = 0;

    /** Creates new form SearchBar */
    public SearchBar(CollIDETextArea textArea) {
        this.textArea = textArea;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        closeButton = new javax.swing.JButton();
        textField = new javax.swing.JTextField();
        nextButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/de/blitzcoder/collide/icons/closetab_mouseover.png"))); // NOI18N
        closeButton.setPreferredSize(new java.awt.Dimension(20, 20));
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(closeButton, gridBagConstraints);

        textField.setPreferredSize(new java.awt.Dimension(100, 25));
        textField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                textFieldCaretUpdate(evt);
            }
        });
        textField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldActionPerformed(evt);
            }
        });
        textField.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                textFieldInputMethodTextChanged(evt);
            }
        });
        textField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textFieldKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textFieldKeyTyped(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(textField, gridBagConstraints);

        nextButton.setText("Vor");
        nextButton.setPreferredSize(new java.awt.Dimension(100, 20));
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(nextButton, gridBagConstraints);

        previousButton.setText("Zurück");
        previousButton.setPreferredSize(new java.awt.Dimension(100, 20));
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(previousButton, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void textFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldActionPerformed
        findNext();
    }//GEN-LAST:event_textFieldActionPerformed

    private void textFieldInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_textFieldInputMethodTextChanged
    }//GEN-LAST:event_textFieldInputMethodTextChanged

    private void textFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_textFieldCaretUpdate
        currentPosition = 0;
        findNext();
    }//GEN-LAST:event_textFieldCaretUpdate

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    private void textFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldKeyTyped
        
    }//GEN-LAST:event_textFieldKeyTyped

    private void textFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldKeyPressed
        if (evt.getKeyCode() == evt.VK_ESCAPE)
            setVisible(false);
    }//GEN-LAST:event_textFieldKeyPressed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        findNext();
    }//GEN-LAST:event_nextButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed
        findNext(true);
    }//GEN-LAST:event_previousButtonActionPerformed

    private void textFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textFieldKeyReleased
        if (evt.getModifiers() == evt.CTRL_MASK && evt.getKeyCode() == evt.VK_ENTER)
            findNext(true);
    }//GEN-LAST:event_textFieldKeyReleased

    public void activate() {
        textField.requestFocus();
        textField.setSelectionStart(0);
        textField.setSelectionEnd(textField.getText().length());
    }

    private void findNext() {
        findNext(false);
    }

    private void findNext(boolean backwards) {
        if (textField.getText().length() != 0) {

            int pos;

            if (backwards)
                pos = textArea.getText().toLowerCase().lastIndexOf(textField.getText().toLowerCase(), currentPosition - 1);
            else
                pos = textArea.getText().toLowerCase().indexOf(textField.getText().toLowerCase(), currentPosition + 1);

            if (pos == -1) {

                if (backwards) {
                    if (currentPosition == textArea.getText().length()) {
                        textField.setBackground(new Color(180, 50, 50));
                    } else {
                        currentPosition = textArea.getText().length();
                        findNext(true);
                    }
                } else {
                    if (currentPosition == 0) {
                        textField.setBackground(new Color(180, 50, 50));
                    } else {
                        currentPosition = 0;
                        findNext();
                    }
                }

            } else {
                textField.setBackground(Color.WHITE);
                textArea.setSelection(new Selection.Range(pos, pos+textField.getText().length()));
                textArea.scrollTo(pos, true);
                currentPosition = pos;
            }
        } else {
            textField.setBackground(Color.WHITE);
            textArea.setSelection((Selection)null);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JTextField textField;
    // End of variables declaration//GEN-END:variables
}
